package com.hl.card.modular.pack.controller;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.CommonUtil;
import com.hl.card.common.util.IdCardUtils;
import com.hl.card.common.util.ResponseData;
import com.hl.card.common.util.StockService;
import com.hl.card.modular.pack.dao.DisMemberInfoMapper;
import com.hl.card.modular.pack.model.DisMemberApplyModel;
import com.hl.card.modular.pack.service.DisMemberApplyService;
import com.hl.card.modular.system.service.ISysDicService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 合伙人申请资料控制器
 * 
 * @author 袁伟
 *
 */
@RestController
@RequestMapping("/api/disMemberApply")
public class MemberApplyController {
	@Autowired
	private DisMemberApplyService disMemberApplyService;
	@Autowired
	private ResponseData responseData;
	@Autowired
    private StockService stockService;
	@Autowired
	private ISysDicService sysDicService;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private DisMemberInfoMapper  disMemberInfoYMapper;
	
	public static  String REDISKEY = "redis_key:stock:rob";

	@RequestMapping("/addDisMemberApply.do")
	public ResponseData addDisMemberApply(DisMemberApplyModel disMemberApply) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(disMemberApply.getLogin_account())) {
			responseData.setStatus("0");
			responseData.setMsg("登录帐号不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(disMemberApply.getUser_id())) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
	
		if(disMemberApply.getType()==null)
		{
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
		if(disMemberApply.getType().equals(0)){  //矿机
			
		if (CommonUtil.isEmpty(disMemberApply.getUser_name())) {
			responseData.setStatus("0");
			responseData.setMsg("姓名不能为空");
			return responseData;
		}
		if (!CommonUtil.validateUserName(disMemberApply.getUser_name())) {
			responseData.setStatus("0");
			responseData.setMsg("姓名必须是16个字符以内,汉字、大小写英文字母、数字及特殊字符（不包含空格）");
			return responseData;
		}
		if (CommonUtil.isEmpty(disMemberApply.getId_no())) {
			responseData.setStatus("0");
			responseData.setMsg("身份证号码不能为空");
			return responseData;
		}
		if (!IdCardUtils.validate(disMemberApply.getId_no())) {
			responseData.setStatus("0");
			responseData.setMsg("身份证号码格式不正确");
			return responseData;
		}
		if(disMemberApply.getRob_num()==null)
		{
			responseData.setStatus("0");
			responseData.setMsg("矿机数量不能为空");
			return responseData;
		}else
		{
			if(disMemberApply.getRob_num()<=0||disMemberApply.getRob_num()>10)
			{
				responseData.setStatus("0");
				responseData.setMsg("本期合伙人开放名额已结束申请");
				return responseData;
			}
		}
		Integer robNum= disMemberInfoYMapper.queryRobNum(disMemberApply.getUser_id());
		if(robNum!=null)
		{
			int totalNum=10-robNum.intValue();  //剩余数量
			if(disMemberApply.getRob_num().intValue()>totalNum)
			{
				responseData.setStatus("0");
				responseData.setMsg("已有"+robNum.intValue()+"台矿机，剩余最大可购入"+totalNum+"台");
				return responseData;
			}
		}
	     int  num=stockService.getStock(REDISKEY);
	     if(disMemberApply.getRob_num()>num)   //如果库存不足
	     {
	    		responseData.setStatus("0");
				responseData.setMsg("本期合伙人矿机数量仅剩"+num+"台");
				return responseData;
	     }
	}else
	{
		User param=new User();
		param.setUserId(disMemberApply.getUser_id());
		User user= userMapper.selectOne(param);
		if(user==null)
		{
			responseData.setStatus("0");
			responseData.setMsg("用户不存在");
			return responseData;
		}else
		{
			disMemberApply.setUser_name(user.getName());
			disMemberApply.setBinding_phone(user.getPhone());
		}
	}
		return disMemberApplyService.addDisMemberApply(disMemberApply);
	}
	
	
	/**
	 * 查询订单支付状态
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/queryIsExistDisMemberApply.do")
	public ResponseData queryIsExistDisMemberApply(String user_id,Integer type) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		if (type==null) {
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.queryIsExistDisMemberApply(user_id,type);
		return responseData;
	}
	
	
	/**
	 * 我的商铺/矿机
	 * @param user_id
	 * @return
	 */
	//@RequestMapping("/queryMemberApplyList.do")
	public ResponseData queryMemberApplyList(String user_id,Integer type,int currentPage,int pageSize) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		if(type==null)
		{
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.queryMemberApplyList(user_id,type,currentPage,pageSize);
		return responseData;
	}
	
	
	
	/**
	 * 取消订单
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/cancelDisMemberApply.do")
	public ResponseData cancelDisMemberApply(String user_id,Integer type) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		if (type==null) {
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.cancelDisMemberApply(user_id,type);
		return responseData;
	}
	
	/**
	 * 订单详情
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/searchDisMemberApplyByUserId.do")
	public ResponseData searchDisMemberApplyByUserId(String user_id,Integer type) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		if (type==null) {
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.searchDisMemberApplyByUserId(user_id,type);
		return responseData;
	}
	
	
	
	/**
	 * 查询推荐码是否存在
	 * @param
	 * @return
	 */
	public ResponseData queryDisMemberInfoCode(String code) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(code)) {
			responseData.setStatus("0");
			responseData.setMsg("推荐码不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.queryDisMemberInfoCode(code);
		return responseData;
	}
	
	
	@RequestMapping("/searchDefaultCode")
	public ResponseData  searchDefaultCode()
	{
		   Map<String, Object> item = sysDicService.selectListByCodeNo("recommend_code", "code");
	        if(item != null) {
	        	String code = (String) item.get("dicValue");
	        	responseData.setStatus("1");
	        	responseData.setMsg("操作成功");
	        	responseData.setData(code);
	        }
	        return responseData; 
	}

	/**
	 * 签约资料
	 * @param user_id
	 * @return
	 */
	@RequestMapping("/queryDisMemberApply.do")
	public ResponseData queryDisMemberApply(String user_id) {
		responseData.setData(null);
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		ResponseData responseData=disMemberApplyService.queryDisMemberApply(user_id);
		return responseData;
	}
	/**
	 * 支付
	 * @param
	 * @return
	 */
	@RequestMapping("/payDisMemberApply.do")
	public ResponseData payDisMemberApply(String user_id,Integer payType,String ip,String openId,String channelId,Integer type,String backUrl,String shopRedirectUrl,String returnUrl) {
		responseData.setData(null); 
		if (CommonUtil.isEmpty(user_id)) {
			responseData.setStatus("0");
			responseData.setMsg("用户ID不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(ip)) {
			responseData.setStatus("0");
			responseData.setMsg("IP不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(payType)) {
			responseData.setStatus("0");
			responseData.setMsg("支付方式不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(backUrl)) {
			responseData.setStatus("0");
			responseData.setMsg("返回跳转URL不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(shopRedirectUrl)) {
			responseData.setStatus("0");
			responseData.setMsg("支付完成跳转URL不能为空");
			return responseData;
		}
		if (CommonUtil.isEmpty(returnUrl)) {
			responseData.setStatus("0");
			responseData.setMsg("支付完成跳转URL不能为空");
			return responseData;
		}
		if (type==null) {
			responseData.setStatus("0");
			responseData.setMsg("标签不能为空");
			return responseData;
		}
	    return disMemberApplyService.payDisMemberApply(user_id,payType,ip,openId,channelId,type,backUrl,shopRedirectUrl,returnUrl);
	}
	/**
	 * 支付异步处理结果
	 * 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/payResult.do")
	public Object payResult(HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
	responseData.setData(null);
	Map<String, Object> map = new HashMap<String, Object>();
	String payOrderId = httpServletRequest.getParameter("payOrderId"); // 支付订单号
	String mchId = httpServletRequest.getParameter("mchId"); // 商户ID
	String mchOrderNo = httpServletRequest.getParameter("mchOrderNo"); // 商户订单号
	String channelId = httpServletRequest.getParameter("channelId"); // 渠道ID
	String amount = httpServletRequest.getParameter("amount"); // 支付金额
	String currency = httpServletRequest.getParameter("currency"); // 货币类型
	String status = httpServletRequest.getParameter("status"); // 支付状态
	String clientIp = httpServletRequest.getParameter("clientIp"); // 客户端IP
	String device = httpServletRequest.getParameter("device"); // 设备
	String subject = httpServletRequest.getParameter("subject"); // 商品标题
	String channelAccount=httpServletRequest.getParameter("channelAccount"); //收款账号
	String channelOrderNo = httpServletRequest
			.getParameter("channelOrderNo"); // 渠道订单号
	String param1 = httpServletRequest.getParameter("param1"); // 扩展参数1
	String param2 = httpServletRequest.getParameter("param2"); // 扩展参数2
	String paySuccTime = httpServletRequest.getParameter("paySuccTime"); // 支付成功时间
	String backType = httpServletRequest.getParameter("backType"); // 保留 暂无值
	String sign = httpServletRequest.getParameter("sign"); // 签名信息
	map.put("payOrderId", payOrderId);
	map.put("mchId", mchId);
	map.put("mchOrderNo", mchOrderNo);
	map.put("channelId", channelId);
	map.put("amount", amount);
	map.put("currency", currency);
	map.put("status", status);
	map.put("clientIp", clientIp);
	map.put("device", device);
	map.put("subject", subject);
	map.put("channelOrderNo", channelOrderNo);
	map.put("param1", param1);
	map.put("param2", param2);
	map.put("paySuccTime", paySuccTime);
	map.put("backType", backType);
	map.put("sign", sign);
	map.put("channelAccount", channelAccount);
	boolean result = disMemberApplyService.payResult(map);
	if (result) {
		return "success";
	} else {
		return "fail";
	}
	}

}
